import 'package:flutter/material.dart';
import 'package:yurun_smartmerchants/models/tobetally_response_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/page/tally_details_page.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
//待理货
class TobeTallyList extends StatefulWidget {
  @override
  _TobeTallyListState createState() => _TobeTallyListState();
}

class _TobeTallyListState extends State<TobeTallyList> {
  List<TobetallyResponseResponseContent> items = List();
  TobetallyResponseResponse tobetallyResponseResponse;

  @override
  void initState() {
    super.initState();
    requestTallyList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colours.gray_ef,
      child: Container(
        margin: EdgeInsets.only(top: AppSize.sp(20)),
        child: ListView.builder(
            itemBuilder: (context, index) {
              return getItem(context, index);
            },
            itemCount: items.length),
      ),
    );
  }

  Widget getItem(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.only(bottom: AppSize.sp(20)),
      child: Column(
        children: <Widget>[
          Container(
            color: Colours.color_f8f8f8,
            width: double.infinity,
            height: AppSize.height(64),
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(
                      left: AppSize.sp(19), right: AppSize.sp(16)),
                  width: AppSize.width(5),
                  height: AppSize.height(28),
                ),
                Text(
                '批次号:' + items[index].batchNo.toString(),
                  style: TextStyle(
                      color: Colours.color_313131, fontSize: AppSize.sp(32)),
                ),
                Spacer(),
                Container(
                  margin: EdgeInsets.only(right: AppSize.sp(32)),
                  child: Text(
                    items[index].createdTime.toString().substring(5),
                    style: TextStyle(
                        fontSize: AppSize.sp(26), color: Colours.color_898989),
                  ),
                )
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: AppSize.height(178),
            color: Colours.white,
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(left: AppSize.sp(16),top: AppSize.sp(16),bottom: AppSize.sp(16)),
                child: Image.network(items[index].categoryUrl,
                width: AppSize.width(178),
                  height: AppSize.height(146),
                  fit: BoxFit.cover,
                ),
                ),
                Container(
                  margin: EdgeInsets.only(left:AppSize.sp(10), top: AppSize.sp(16)),
                  child:Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(items[index].categoryName.toString(), style: TextStyle(fontSize: 18),),
                      Text(items[index].preWeight.toString() + '/公斤',style: TextStyle(color: Colors.red,),),
                    ],
                  ),
                ),
                Spacer(),
                Container(
                  margin: EdgeInsets.only(right: AppSize.sp(30)),
                  child:RaisedButton(
                    child: Text('理货'),
                    color: Colors.red,
                    textColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(5),
                    ),
                    onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                      return TallyDetailPage(content: items[index]);
                    }));
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  void requestTallyList() async {
    Map<String, dynamic> parameter = new Map();
    parameter['status'] = 0;
    parameter['pageNum'] = 0;
    parameter['pageSize'] = 15;
    ApiInterface.tallyList(parameter).then((data) {
        tobetallyResponseResponse = TobetallyResponseResponse().fromJson(data);
        handleData(tobetallyResponseResponse);
    }).catchError((error) {

    });
  }

  void handleData(TobetallyResponseResponse tobetallyResponseResponse) {
    if(tobetallyResponseResponse.content != null && tobetallyResponseResponse.content.length > 0){
    items = tobetallyResponseResponse.content;
    }else{
      //数据为空的处理

    }
    setState(() {});
  }


}
